/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.zzz.view.managerControl;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import org.zzz.controller.CourseController;
import org.zzz.controller.TeacherController;
import org.zzz.controller.TeacherCourseController;
import org.zzz.entity.Course;
import org.zzz.entity.Teacher;
import org.zzz.entity.TeacherCourse;
import org.zzz.entity.vo.CourseShowVo;
import org.zzz.utils.Result;
import org.zzz.utils.ResultEnum;
import org.zzz.view.util.UiUtil;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 *
 * @author AFun
 */
public class manageCouSelect extends javax.swing.JFrame {

    /**
     * Creates new form manageCouSelect
     */
    public manageCouSelect() {
        initComponents();
        init();         //初始化
    }
    private void init(){
        this.setTitle("课程操作");
        UiUtil.setFrameCenter(this);
        this.setResizable(false);
        this.m_CouNum="";
        this.m_CouName="";
        this.m_couTeacher="";
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox<>();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        chaxun = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("课程号/课程名");

        jLabel2.setText("根据");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "课程号", "课程名" }));

        jButton1.setText("返回");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("修改");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("删除");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        chaxun.setText("查询");
        chaxun.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                chaxunActionPerformed(e);
            }
        });

        jTable1.setBackground(new java.awt.Color(214, 217, 223));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                        {null, null, null}
                },
                new String [] {
                        "课程号", "课程名", "任课教师"
                }
        ) {
            boolean[] canEdit = new boolean [] {
                    false, false, false
            };
            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addGroup(layout.createSequentialGroup()
                                                        .addGap(69, 69, 69)
                                                        .addComponent(jButton2)
                                                        .addGap(54, 54, 54)
                                                        .addComponent(jButton3)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(jButton1))
                                                .addGroup(layout.createSequentialGroup()
                                                        .addGap(60, 60, 60)
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(jLabel2)
                                                                .addComponent(jLabel1))
                                                        .addGap(29, 29, 29)
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGroup(layout.createSequentialGroup()
                                                                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addGap(60, 60, 60)
                                                                        .addComponent(chaxun)))))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(42, 42, 42)
                                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(52, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(56, 56, 56)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(39, 39, 39)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(chaxun))
                                .addGap(12, 12, 12)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jButton2)
                                        .addComponent(jButton3)
                                        .addComponent(jButton1))
                                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        manager_couMessage mcm=new manager_couMessage();
        this.dispose();
        mcm.setVisible(true);
    }

    private void chaxunActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        //根据的查询类型
        DefaultTableModel model = (DefaultTableModel)this.jTable1.getModel();
        if(model.getRowCount()!=0){
            model.removeRow(0);
            //所有信息移除
            this.m_CouNum="";
            this.m_CouName="";
            this.m_couTeacher="";
        }
        String selectedClass = (String)this.jComboBox1.getSelectedItem();
        String text = this.jTextField1.getText();
        CourseController courseController = new CourseController();
        Result<CourseShowVo> courseResult = null;
        if ("课程号".equals(selectedClass)){
            courseResult = courseController.getCourseVoByIdOrName("cid",text);
        }else{
            courseResult = courseController.getCourseVoByIdOrName("cname",text);
        }

        if (courseResult.getStatus() == ResultEnum.SUCCESS.getStatus()){
            CourseShowVo courseShowVo = courseResult.getData();
            model.addRow(new Object[]{courseShowVo.getId(),courseShowVo.getName(),courseShowVo.getTeacherName()});
            this.m_CouNum=courseShowVo.getId();
            this.m_CouName=courseShowVo.getName();
            this.m_couTeacher=courseShowVo.getTeacherName();
        }else{
            JOptionPane.showMessageDialog(this, "查无此数据");
        }


    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        //修改
//        if(!(this.m_couTeacher.equals("")||this.m_CouNum.equals("")||this.m_CouName.equals(""))){
        if(!(this.m_CouNum.equals("")||this.m_CouName.equals(""))){
//            Result<List<Teacher>> teacherResult = new TeacherController().getTeacherByName(this.m_couTeacher);
//            String tid = null;
//            if (teacherResult.getStatus() == ResultEnum.SUCCESS.getStatus()){
//                Teacher teacher = teacherResult.getData().get(0);
//
//            }
            managerCosAlter mca=new managerCosAlter(this.m_CouNum,this.m_CouName,this.m_couTeacher);
            this.dispose();
            mca.setVisible(true);
        }else{
            JOptionPane.showMessageDialog(this, "没有此人，叫在下如何修改？");
        }

    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
//        if(!(this.m_couTeacher.equals("")||this.m_CouNum.equals("")||this.m_CouName.equals(""))){
        if(!(this.m_CouNum.equals("")||this.m_CouName.equals(""))){
            //询问是否删除？
            if (JOptionPane.showConfirmDialog(null, "是否删除？", "Message", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                //是：将文本框
                CourseController courseController = new CourseController();
                TeacherCourseController teacherCourseController = new TeacherCourseController();
                String cid = this.jTextField1.getText();
                courseController.deleteCourse(cid);
                Result<TeacherCourse> tcByCid = teacherCourseController.getTCByCid(cid);
                if (tcByCid.getStatus()== ResultEnum.SUCCESS.getStatus()){
                    teacherCourseController.deleteTCById(tcByCid.getData().getId());
                }
                JOptionPane.showMessageDialog(null, "删除成功！");
                this.jTextField1.setText("");
                //删除成功，将表数据清空
                DefaultTableModel model = (DefaultTableModel)this.jTable1.getModel();
                if(model.getRowCount()!=0){
                    model.removeRow(0);
                    //所有信息移除
                    this.m_CouNum="";
                    this.m_CouName="";
                    this.m_couTeacher="";
                }
            }
        }else{
            JOptionPane.showMessageDialog(this, "没有此人，叫在下如何删除？");
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(manageCouSelect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(manageCouSelect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(manageCouSelect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(manageCouSelect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new manageCouSelect().setVisible(true);
            }
        });
    }

    //存储查询到的成员变量
    private String m_CouNum;
    private String m_CouName;
    private String m_couTeacher;

    // Variables declaration - do not modify
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton chaxun;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    // End of variables declaration
}
